﻿namespace EFMLEditor.Internal
{
    using System.Collections.Generic;
    using System.Globalization;
    using System.IO;
    using System.Xml;

    public class ProjectBuilder
    {
        public static void Build(string path, string projectname, string icon, string author, string version, string copyright, string description, string website)
        {
            var p = path + @"\" + projectname + @"\";
            Directory.CreateDirectory(p);
            Directory.CreateDirectory(p + "Properties");
            Directory.CreateDirectory(p + @"Properties\Strings");
            Directory.CreateDirectory(p + @"Properties\Resources");
            Directory.CreateDirectory(p + "Build");

            File.WriteAllText(p + @"Properties\Strings\" + CultureInfo.CurrentCulture.Name + ".string", StringBuilder.Build());

            File.WriteAllText(p + projectname + ".proj", ProjectFileBuilder.Build());

            File.WriteAllText(p + "main.window", WindowBuilder.Build(projectname));
            File.Create(p + "main.code");

            var vals = new Dictionary<string, string> { { "appname", projectname }, { "icon", icon }, {"author", author}, {"version", version}, {"copyright", copyright}, {"description", description}, {"website", website}, {"praebuild", ""}, {"postbuild", ""} };

            File.WriteAllText(p + @"\Properties\application.props", PropsBuilder.Build(vals));
        }
    }
    public class ProjectFileBuilder
    {
        public static string Build()
        {
            var doc = new XmlDocument();
            var root = doc.CreateElement("project");
            var props = doc.CreateElement("properties");

            props.SetAttribute("path", @"\Properties\application.props");

            var strings = doc.CreateElement("strings");
            var resources = doc.CreateElement("resources");
            var windows = doc.CreateElement("windows");

            var defaultWin = doc.CreateElement("source");
            defaultWin.SetAttribute("path", "main.window");
            defaultWin.SetAttribute("code", "main.code");

            var defaultString = doc.CreateElement("source");
            defaultString.SetAttribute("path", CultureInfo.CurrentCulture.Name + ".string");
            strings.AppendChild(defaultString);

            windows.AppendChild(defaultWin);

            props.AppendChild(strings);
            props.AppendChild(resources);

            root.AppendChild(windows);
            root.AppendChild(props);

            doc.AppendChild(root);

            var ms = new MemoryStream();
            doc.Save(ms);
            return System.Text.Encoding.ASCII.GetString(ms.ToArray());
        }
    }
    public class StringBuilder
    {
        public static string Build()
        {
            var doc = new XmlDocument();
            var root = doc.CreateElement("strings");

            var defaults = doc.CreateElement("string");
            defaults.SetAttribute("name", "appname");
            defaults.SetAttribute("value", "default");


            root.AppendChild(defaults);
            doc.AppendChild(root);

            var ms = new MemoryStream();
            doc.Save(ms);
            return System.Text.Encoding.ASCII.GetString(ms.ToArray());
        }
    }
    public class PropsBuilder
    {
        public static string Build(Dictionary<string, string> vals)
        {
            var doc = new XmlDocument();
            var root = doc.CreateElement("props");

            foreach (var val in vals)
            {
                var name = doc.CreateElement(val.Key);
                name.InnerText = val.Value;

                root.AppendChild(name);
            }
            
            doc.AppendChild(root);

            var ms = new MemoryStream();
            doc.Save(ms);
            return System.Text.Encoding.ASCII.GetString(ms.ToArray());
        }
    }
    public class WindowBuilder
    {
        public static string Build(string appname)
        {
            var doc = new XmlDocument();
            var root = doc.CreateElement("window");
            var head = doc.CreateElement("head");
            var body = doc.CreateElement("body");

            var title = doc.CreateElement("meta");
            title.SetAttribute("name", "Title");
            title.SetAttribute("value", appname);

            head.AppendChild(title);

            root.AppendChild(head);
            root.AppendChild(body);

            doc.AppendChild(root);

            var ms = new MemoryStream();
            doc.Save(ms);
            return System.Text.Encoding.ASCII.GetString(ms.ToArray());
        }
    }
}